package com.driving.single.common.content.service.impl;

import com.driving.single.common.content.service.CommonContentService;
import com.driving.single.common.domain.TbContent;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.mapper.TbContentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CommonContentServiceImpl implements CommonContentService {
    @Autowired
    private TbContentMapper tbContentMapper;

    /**
     * 根据分类主键分页查询内容
     *
     * @param categoryId 分类主键
     */
    @Override
    public PageResult selectTbContentAllByCategoryIdWithPage(Long categoryId, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);

        Example example = new Example(TbContent.class);
        example.createCriteria().andEqualTo("categoryId", categoryId);
        List<TbContent> list = this.tbContentMapper.selectByExample(example);

        PageInfo<TbContent> pageInfo = new PageInfo<>(list);

        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(page);
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setResult(list);

        return pageResult;
    }

    /**
     * 根据分类主键查询内容
     *
     * @param categoryId 分类主键
     */
    @Override
    public List<TbContent> selectTbContentAllByCategoryId(Long categoryId) {
        Example example = new Example(TbContent.class);
        example.createCriteria().andEqualTo("categoryId", categoryId);
        return this.tbContentMapper.selectByExample(example);
    }
}
